import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Leet51 {
	public static void main(String[] args) {
		List<List<String>> list = Leet51.solveNQueens(5);
	    for(List<String> t :list) {
	    	for(String tw :t) {
	    		System.out.print(tw+"||");
	    	}
	    	System.out.println();
	    }
	
	}
	static List<int[][]> list;
	static List<List<String>> re;
	static public List<List<String>> solveNQueens(int n) {
		list = new ArrayList<>();
		re = new ArrayList<>();
		int[][] temp = new int[n][n];
		for(int i = 0;i<n;i++) {
			temp[0][i] = 1;
			dfs(temp,1,n);
			temp = new int[n][n];
		}
		return re;
    }
	static void dfs(int[][] temp,int l,int n) {
		if(l==n) {
			List<String> ss = new ArrayList<>();
			for(int[] b :temp) {
				StringBuilder sb = new StringBuilder();
				for(int c:b) {
					if(c==0)
						sb.append(".");
					if(c==1)
						sb.append("Q");
				}
				ss.add(sb.toString());
				sb = new StringBuilder();
			}
			re.add(ss);	
			return;
		}
		for(int i = 0;i<n;i++) {
			if(check(i,l,temp,n)) {
				temp[l][i] = 1;
				dfs(temp,l+1,n);
				temp[l][i] = 0;
			}
		}
		return;
	}
	static boolean check(int x ,int y,int[][] temp,int n) {
		
		for(int i = 0;i<n;i++) {
			if(temp[i][x]==1) {
				return false;
			}
				
		}
	    for (int i = y - 1, j = x - 1; i >=0 && j >= 0; i--, j--) {
	        if (temp[i][j] == 1) {
	            return false;
	        }
	    }
	    for(int i = y - 1, j = x + 1; i >= 0 && j < n; i--, j++) {
	        if (temp[i][j] == 1) {
	            return false;
	        }
	    }
		return true;
	}
}
